﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using Abp.UI;
using Camc.Quality.Common;
using System;
using System.Linq;



namespace Camc.Quality.DataDictionary
{
    public class SysCodeAppService : QualityAppServiceBase
    {
        private readonly IRepository<Sys_Code, Guid> _sysCodeRepository;
        public SysCodeAppService(IRepository<Sys_Code, Guid> sysCodeRepository)
        {

            _sysCodeRepository = sysCodeRepository;
        }
        /// <summary>
        /// 新增或修改数据字典信息
        /// </summary>
        /// <param name="sysCode"></param>
        public ResultMessage CreateOrUpdateCode(SysCodeDto sysCode)
        {
            ResultMessage resultMessage = new ResultMessage();
            try
            {
                var code = ObjectMapper.Map<Sys_Code>(sysCode);
                if (code.Id == Guid.Empty)
                {
                    if (code.PPtr != Guid.Empty)
                    {
                        //获取CodeId 数据字典类别
                        var codeId = _sysCodeRepository.Get(sysCode.PPtr).CodeId;
                        code.CodeId = codeId;
                        code.Sort = _sysCodeRepository.GetAll().Where(d => d.PPtr == code.PPtr).Count() + 1;

                        var tempCode = _sysCodeRepository.Count(d => d.PPtr == code.PPtr && d.Description == sysCode.Description);
                        if (tempCode > 0)
                        {
                            resultMessage.code = 1;
                            resultMessage.Message = "该类别已含有所创建的子级名称";
                            return resultMessage;
                            //throw new UserFriendlyException("该类别已含有所创建的子级名称!不可重复创建。");
                        }
                    }
                    else
                    {
                        code.Sort = _sysCodeRepository.GetAll().Where(d => d.PPtr == Guid.Empty).Count() + 1;
                    }
                }
                else
                {
                    var tempCode = _sysCodeRepository.Count(d => d.PPtr == code.PPtr && d.Id != sysCode.Id && d.Description == sysCode.Description);
                    if (tempCode > 0)
                    {
                        resultMessage.code = 1;
                        resultMessage.Message = "该类别已含有所创建的子级名称!不可重复修改。";
                        return resultMessage;
                        //throw new UserFriendlyException("该类别已含有所创建的子级名称!不可重复修改。");
                    }
                }
                _sysCodeRepository.InsertOrUpdate(code);
                resultMessage.code = 0;
                return resultMessage;
            }
            catch (Exception ex)
            {
                resultMessage.code = 1;
                resultMessage.Message = ex.Message;
                return resultMessage;
            }
        }
        /// <summary>
        /// 根据Id删除数据字典
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ResultMessage DeleteCodeById(Guid Id)
        {
            ResultMessage resultMessage = new ResultMessage();
            var code = _sysCodeRepository.FirstOrDefault(Id);
            if (code != null)
            {
                _sysCodeRepository.Delete(code);
                resultMessage.code = 0;
                return resultMessage;
            }
            else
            { 
                resultMessage.code = 1;
                return resultMessage;
            }
        }
        /// <summary>
        /// 根据父节点Id获取子节点
        /// </summary>
        /// <param name="pptr"></param>
        /// <returns></returns>

        public PagedResultDto<Sys_Code> GetCodeListByParentId(SysCodeDto dto)
        {
            var codeList = _sysCodeRepository.GetAll().Where(d => d.PPtr == dto.PPtr);
            int count = 0;
            if (codeList.Count() > 0)
            {
                count = codeList.Count();
                codeList = codeList.OrderByDescending(d => d.CreationTime).PageBy(dto);
            }
            return new PagedResultDto<Sys_Code>(count, codeList.ToList());
        }

    }
}
